package com.example.common.tools.text

import android.text.Spannable
import android.text.SpannableString
import android.text.style.AbsoluteSizeSpan
import android.text.style.ForegroundColorSpan
import android.widget.TextView
import com.example.common.R
import com.example.common.tools.TooldUtils
import com.example.common.tools.getColor

class TextUtil {

    companion object {
        val instance by lazy(LazyThreadSafetyMode.NONE) {
            TextUtil()
        }
    }

    //"用户${name} 成功录制${index}次"
    fun setTextString(view: TextView) {
        var prompt = view.text.toString()
        val text = view
        val span: Spannable = SpannableString(text.text.toString())
        span.setSpan(
            ForegroundColorSpan(R.color.light_blue.getColor()), prompt.indexOf("户") + 1, prompt.indexOf("成"),
            Spannable.SPAN_INCLUSIVE_EXCLUSIVE
        )
        span.setSpan(
            ForegroundColorSpan(R.color.red_pai.getColor()), prompt.indexOf("制") + 1, prompt.indexOf("次"),
            Spannable.SPAN_INCLUSIVE_EXCLUSIVE
        )
        span.setSpan(
            AbsoluteSizeSpan(TooldUtils.dp2px(18f)),
            prompt.indexOf("制") + 1, prompt.indexOf("次"),
            Spannable.SPAN_INCLUSIVE_INCLUSIVE
        )
        view.text = span
    }
}